package mock.action;

import java.util.Map;

import mock.bo.ChangePIN;
import mock.bo.ChangePINImp;
import mock.bo.Logging;
import mock.bo.LoggingImp;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChangePINAction extends ActionSupport /*implements SessionAware*/{
	
	private ChangePIN changePINBO = new ChangePINImp();
	private Logging logBO = new LoggingImp();
	
	private String txtNewPIN;
	private String txtReNewPIN;
	//private Map<String, Object> session;
	Map<String, Object> session = ActionContext.getContext().getSession();
	
	/*@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}*/

	public String getTxtReNewPIN() {
		return txtReNewPIN;
	}

	public void setTxtReNewPIN(String txtReNewPIN) {
		this.txtReNewPIN = txtReNewPIN;
	}

	public String getTxtNewPIN() {
		return txtNewPIN;
	}

	public void setTxtNewPIN(String txtNewPIN) {
		this.txtNewPIN = txtNewPIN;
	}

	
	/**
	 * kiem tra xem PIN nhap co hop le hay khong
	 * @return
	 */
	public String checkPIN(){
		//kiem tra PIN co it nhat 6 phan tu
		//kiem tra PIN khong trung voi PIN cu
		String cardNo = (String) session.get("cardNo");
		boolean chk = changePINBO.checkNewPIN(txtNewPIN, cardNo);
		if(chk == true){
			session.put("newPIN", txtNewPIN);
			return "checkPINSuccess";
		}
		//dua new PIN len session
		return "checkPINFail";
	}

	/**
	 * kiem tra Re-PIN co trung voi PIN moi nhap hay khong
	 * @return
	 */
	public String process(){
		//lay session
		String newPIN = (String) session.get("newPIN");
		String cardNo = (String) session.get("cardNo");
		int atmID = (int) session.get("ATMID");
		//kiem tra ma PIN nhap lai co chinh xac voi ma PIN ban dau hay chua
		if(newPIN.equals(txtReNewPIN)){
			changePINBO.changePIN(txtReNewPIN, cardNo);
			//ghi log
			logBO.logChangePIN(atmID, cardNo);
			//xoa session
			session.remove("newPIN");
			return "processSuccess";
		}
		//goi ham o BO thuc hien viec thay doi ma PIN
		return "processFail";
	}
	
}
